<template>
  <div>
    <hips-button @click="handleSave1"> 顺序调用飞搭事件 </hips-button>
    <hips-button @click="handleSave2"> 无序调用飞搭事件 </hips-button>
  </div>
</template>

<script>
import { Button } from '@hips/vue-ui';

export default {
  name: 'Rate',
  components: {
    [Button.name]: Button,
  },
  props: {
    event1: Object, //
    event2: Object,
    handleRunEvent: Function, // 预置属性-调用飞搭事件流
    handleChangeOutputParams: Function, // 预置属性-出参到 自定义组件
    setEventFunction: Function, // 预置属性-设置自定义组件函数给飞搭调用
  },
  data() {
    return {};
  },
  mounted() {
    this.setEventFunction('fn1', this.fn1);

    this.handleChangeOutputParams({
      name: 'outParam1',
      value: '修改后的自定义出参1',
    });

    this.handleChangeOutputParams({
      name: 'outParam2',
      value: '修改后的自定义出参2',
    });

    this.handleChangeOutputParams({
      name: 'outParam3',
      value: '修改后的自定义出参3',
    });

    console.log('>>> FEAT CustomComponent mounted');
  },
  methods: {
    fn1() {
      this.handleChangeOutputParams({
        name: 'outParam2',
        value: 'aaaaaa',
      });
      this.handleChangeOutputParams({
        name: 'outParam3',
        value: 'bbbb',
      });

      return Promise.resolve();
    },
    /**
     * 同步调用飞搭事件流
     */
    async handleSave1() {
      console.log('>>> FEAT handleSave1 CustomComponent call event1');
      await this.handleRunEvent(
        this.event1.flowCode,
        () => {
          console.log(
            '>>> FEAT handleSave1 CustomComponent call event1 success'
          );
          return Promise.resolve();
        },
        () => {
          console.log('>>> FEAT handleSave1 CustomComponent call event1 fail');
          return Promise.reject();
        }
      );

      console.log('>>> FEAT handleSave1 CustomComponent call event2');
      await this.handleRunEvent(
        this.event2.flowCode,
        () => {
          console.log(
            '>>> FEAT handleSave1 CustomComponent call event2 success'
          );
          return Promise.resolve();
        },
        () => {
          console.log('>>> FEAT handleSave1 CustomComponent call event2 fail');
          return Promise.reject();
        }
      );

      console.log('>>> FEAT handleSave1 CustomComponent call end');
    },
    /**
     * 异步调用飞搭事件流
     */
    handleSave2() {
      console.log('>>> FEAT handleSave2 CustomComponent call event1');
      this.handleRunEvent(
        this.event1.flowCode,
        () => {
          console.log(
            '>>> FEAT handleSave2 CustomComponent call event1 success'
          );
          return Promise.resolve();
        },
        () => {
          console.log('>>> FEAT handleSave2 CustomComponent call event1 fail');
          return Promise.reject();
        }
      );

      console.log('>>> FEAT handleSave2 CustomComponent call event2');
      this.handleRunEvent(
        this.event2.flowCode,
        () => {
          console.log(
            '>>> FEAT handleSave2 CustomComponent call event2 success'
          );
          return Promise.resolve();
        },
        () => {
          console.log('>>> FEAT handleSave2 CustomComponent call event2 fail');
          return Promise.reject();
        }
      );

      console.log('>>> FEAT handleSave2 CustomComponent call end');
    },
  },
};
</script>
